package org.rency.crawler.plugins.service.impl;

import com.linkedin.urls.Url;
import com.linkedin.urls.detection.UrlDetector;
import com.linkedin.urls.detection.UrlDetectorOptions;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.rency.crawler.plugins.service.UrlDetectorPlugin;
import org.rency.crawler.plugins.url.NormalizeUrl;
import org.springframework.stereotype.Component;

/**
 * 描述: ${TODO}<br>
 * 最近修改时间:2017/12/15 11:42<br>
 *
 * @author CaiyuRen
 * @version V1.0
 * @since 2017/12/15 11:42
 */
@Component
public class UrlDetectorPluginImpl implements UrlDetectorPlugin {

  @Override
  public NormalizeUrl normalize(String uri) {
    UrlDetector parser = new UrlDetector(uri, UrlDetectorOptions.Default);
    List<Url> found = parser.detect();
    if(CollectionUtils.isEmpty(found)){
      return null;
    }
    Url url = found.get(0);
    NormalizeUrl normalizeUrl = new NormalizeUrl();
    normalizeUrl.setFullUri(url.getFullUrl());
    normalizeUrl.setHost(url.getHost());
    normalizeUrl.setNormalizeUri(url.getFullUrlWithoutFragment());
    normalizeUrl.setOrgiUri(uri);
    normalizeUrl.setPath(url.getPath());
    normalizeUrl.setScheme(url.getScheme());
    return normalizeUrl;
  }
}
